//
//  OCSSPageIndexView.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/11/1.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit

class OCSSPageIndexView: UIView, UIScrollViewDelegate {
    
    private var titleScrollView:UIScrollView?;
    private var mainScrollView:UIScrollView?;
    private var selectTitle:UILabel?;
    var titles:[String]?;
    var controllers:[UIViewController]?;
    var titleH = 44.0;
    var currentIndex = 0{
        didSet{
            if currentIndex == 0 {
                scrollViewDidEndScrollingAnimation(mainScrollView!);
            }else{
                mainScrollView?.setContentOffset(CGPoint.init(x: CGFloat(currentIndex)*kWidth, y: 0), animated: true);
            }
            
        }
    };
    var titleSelectColor:UIColor? = UIColor.init(red: 0, green: 155/255.0, blue: 255/255.0, alpha: 1);
    var titleNormalColor:UIColor? = UIColor.darkGray;
    var titleNormalFont:CGFloat = 16;
    var titleSelectFont:CGFloat = 16;
    
    init(frame:CGRect, titles:[String], controllers:[UIViewController]) {
        super.init(frame: frame);
        
        self.titles = titles;
        self.controllers = controllers;
        initUI();
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        
        initUI();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initUI() -> Void {
        var titleW:CGFloat = 0;
        titleW = kWidth / CGFloat((titles?.count)!);
        if (titleW < 65) {
            titleW = 65;
        }
        titleScrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: kWidth, height: 44));
        titleScrollView?.backgroundColor = UIColor.white;
        titleScrollView?.delegate = self;
        titleScrollView?.contentSize = CGSize.init(width: titleW*CGFloat((titles?.count)!), height: 44);
        titleScrollView?.showsVerticalScrollIndicator = false;
        titleScrollView?.showsHorizontalScrollIndicator = false;
        self.addSubview(titleScrollView!);
        for i in 0...self.titles!.count-1 {
            let titleLab = UILabel.init(frame: CGRect.init(x: titleW*CGFloat(i), y: 0, width: titleW, height: 44));
            titleLab.textAlignment = NSTextAlignment.center;
            titleLab.text = self.titles?[i];
            titleLab.textColor = titleNormalColor;
            titleLab.tag = 10+i;
            titleLab.font = UIFont.init(name: "PingFangSC-Regular", size: 15);
            titleLab.isUserInteractionEnabled = true;
            titleLab.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(titleDidSelected(ges: ))));
            titleScrollView?.addSubview(titleLab);
        }
        mainScrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: 44, width: kWidth, height: kHeight-64));
        mainScrollView?.isPagingEnabled = true;
        mainScrollView?.delegate = self;
        mainScrollView?.contentSize = CGSize.init(width: kWidth*CGFloat((controllers?.count)!), height: kHeight-64);
        mainScrollView?.showsHorizontalScrollIndicator = false;
        mainScrollView?.showsVerticalScrollIndicator = false;
        self.addSubview(mainScrollView!);
        mainScrollView?.setContentOffset(CGPoint.init(x: CGFloat(currentIndex)*kWidth, y: 0), animated: true);
    }
    @objc func titleDidSelected(ges:UITapGestureRecognizer) -> Void {
        let titleLab = ges.view as! UILabel;
        let index = titleLab.tag-10;
        mainScrollView?.setContentOffset(CGPoint.init(x: CGFloat(index)*kWidth, y: 0), animated: true);
    }
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        if scrollView == mainScrollView {
            
            let index = Int(scrollView.contentOffset.x/kWidth);
            let currentVC = controllers![Int(index)] as! OCSSContentOrderListPage;
            currentVC.sourceType = SourceType(rawValue: index)!;
            currentVC.view.frame = CGRect.init(x: kWidth*CGFloat(index), y: 0, width: kWidth, height: scrollView.frame.size.height);
            mainScrollView?.addSubview(currentVC.view);
            selectTitle?.textColor = titleNormalColor;
            selectTitle?.font = UIFont.init(name: "PingFangSC-Regular", size: titleNormalFont);
            let titleLab = titleScrollView?.viewWithTag(10+index) as! UILabel;
            titleLab.font = UIFont.init(name: "PingFangSC-Semibold", size: titleSelectFont);
            titleLab.textColor = titleSelectColor;
            selectTitle = titleLab;
        }
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == mainScrollView {
            scrollViewDidEndScrollingAnimation(scrollView);
        }
    }
}
